<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="日期" size="small">
            <el-date-picker
              v-model="searchForm.checkTime"
              type="date"
              value-format="timestamp"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="700px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="140px">
          <el-row>
            <el-col class="other">
              <el-form-item label="电梯外走廊及绿植" size="small" prop="elevatorHealth" label-width="">
                <el-select v-model="teamRow.elevatorHealth" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="前台桌面" size="small" prop="frontDeskDesktop">
                <el-select v-model="teamRow.frontDeskDesktop" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="前台前方走廊" size="small" prop="frontDeskCorridor">
                <el-select v-model="teamRow.frontDeskCorridor" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="前台周边所有灯光" size="small" prop="ambientLight">
                <el-select v-model="teamRow.ambientLight" placeholder="请选择">
                  <el-option
                    v-for="item in global.isinjureList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="门铃(内/外)" size="small" prop="doorbell">
                <el-select v-model="teamRow.doorbell" placeholder="请选择">
                  <el-option
                    v-for="item in global.isinjureList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="快递柜" size="small" prop="expressArk">
                <el-select v-model="teamRow.expressArk" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="厕所洗手台" size="small" prop="bathroomSink">
                <el-select v-model="teamRow.bathroomSink" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="厕所纸和洗手液" size="small" prop="paperSanitizer">
                <el-select v-model="teamRow.paperSanitizer" placeholder="请选择">
                  <el-option
                    v-for="item in global.isampleList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="饮水机水桶摆放" size="small" prop="bucketPlacement">
                <el-select v-model="teamRow.bucketPlacement" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item label="公司玻璃门" size="small" prop="glassDoor">
                <el-select v-model="teamRow.glassDoor" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="打印机区域" size="small" prop="printerArea">
                <el-select v-model="teamRow.printerArea" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="打印纸、备用纸" size="small" prop="printingPaper">
                <el-select v-model="teamRow.printingPaper" placeholder="请选择">
                  <el-option
                    v-for="item in global.isampleList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="打印机" size="small" prop="printer">
                <el-select v-model="teamRow.printer" placeholder="请选择">
                  <el-option
                    v-for="item in global.isinjureList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="饮水机" size="small" prop="waterDispenser">
                <el-select v-model="teamRow.waterDispenser" placeholder="请选择">
                  <el-option
                    v-for="item in global.isinjureList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="会客区桌面" size="small" prop="receptionDesktop">
                <el-select v-model="teamRow.receptionDesktop" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="会议室遥控器位置" size="small" prop="positionController">
                <el-select v-model="teamRow.positionController" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="大、小会议室环境" size="small" prop="roomEnvironment">
                <el-select v-model="teamRow.roomEnvironment" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="鲍工办公室" size="small" prop="office">
                <el-select v-model="teamRow.office" placeholder="请选择">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col class="other">
              <el-form-item label="会客区纸杯、糖、纸巾(垃圾桶位置)" size="small" prop="receptionLocation">
                <el-select v-model="teamRow.receptionLocation" placeholder="请选择" class="selectclass">
                  <el-option
                    v-for="item in global.isampleList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col class="other">
              <el-form-item label="餐厅桌面、台面、微波炉、地面" size="small" prop="restaurantDesktop">
                <el-select v-model="teamRow.restaurantDesktop" placeholder="请选择" class="selectclass">
                  <el-option
                    v-for="item in global.iscleanList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col class="other">
              <el-form-item prop="checkMan" label="巡视人" size="small">
                <el-input v-model="teamRow.checkMan"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item prop="checkTime" label="日期" size="small">
                <el-date-picker
                  v-model="teamRow.checkTime"
                  type="date"
                  class="dataclass"
                  value-format="timestamp"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col class="other">
              <el-form-item label="备注说明" size="small">
                <el-input type="textarea" v-model="teamRow.remark"
                          class="selectclass"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading">
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.checkId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    addPatrol,
    deletePatrol,
    getList,
    selectPatrol,
    updatePatrol
  } from '@/api/companypatrol'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const checkTime = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'checkTime', label: '日期', width: '100', component: checkTime },
          { prop: 'checkMan', label: '巡视人', minWidth: '20%' },
          { prop: 'glassDoor', label: '公司玻璃门', minWidth: '15%' },
          { prop: 'elevatorHealth', label: '电梯外走廊及绿植', minWidth: '20%' },
          { prop: 'expressArk', label: '快递柜', minWidth: '20%' },
          { prop: 'printingPaper', label: '打印纸、备用纸', minWidth: '20%' },
          { prop: 'restaurantDesktop', label: '餐厅桌面、台面、微波炉、地面', minWidth: '25%' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增公司环境巡视记录',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        pca: pca,
        rules: {
          checkTime: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ],
          checkMan: [
            { required: true, message: '请输入巡视人', trigger: 'change' }
          ],
          expressArk: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          glassDoor: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          printingPaper: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          restaurantDesktop: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          elevatorHealth: [
            { required: true, message: '请选择', trigger: 'change' }
          ]
        },
        visible: false
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        this.outerVisible = true
        this.dialogTitle = '新增公司环境巡视记录'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
      },
      editTeam(checkId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改公司环境巡视记录'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          checkId: checkId
        }
        selectPatrol(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = {
          teamId: this.teamRow.teamId,
          checkTime: this.teamRow.checkTime,
          elevatorHealth: this.teamRow.elevatorHealth,
          glassDoor: this.teamRow.glassDoor,
          frontDeskDesktop: this.teamRow.frontDeskDesktop,
          frontDeskCorridor: this.teamRow.frontDeskCorridor,
          ambientLight: this.teamRow.ambientLight,
          doorbell: this.teamRow.doorbell,
          expressArk: this.teamRow.expressArk,
          bathroomSink: this.teamRow.bathroomSink,
          paperSanitizer: this.teamRow.paperSanitizer,
          office: this.teamRow.office,
          printerArea: this.teamRow.printerArea,
          printingPaper: this.teamRow.printingPaper,
          printer: this.teamRow.printer,
          waterDispenser: this.teamRow.waterDispenser,
          bucketPlacement: this.teamRow.bucketPlacement,
          receptionDesktop: this.teamRow.receptionDesktop,
          receptionLocation: this.teamRow.receptionLocation,
          restaurantDesktop: this.teamRow.restaurantDesktop,
          positionController: this.teamRow.positionController,
          roomEnvironment: this.teamRow.roomEnvironment,
          checkMan: this.teamRow.checkMan || '',
          remark: this.teamRow.remark || ''
        }
        if (!this.dialogType) {
          params.checkId = this.teamRow.checkId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addPatrol(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updatePatrol(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        console.log(this.$refs[`popover-${scope.$index}`])
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          checkId: scope.row.checkId,
          isDel: 1
        }
        deletePatrol(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      addressChange() {
        this.teamRow.provinceId = this.teamRow.addressId[0]
        this.teamRow.cityId = this.teamRow.addressId[1]
        this.teamRow.address = this.pca[86][this.teamRow.addressId[0]] + this.pca[this.teamRow.addressId[0]][this.teamRow.addressId[1]]
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .textareaclass {
    width: 460px;
    height: 200px;
  }

  .selectclass {
    width: 460px;
  }

  .dataclass {
    width: 160px;
  }

  .other {
    width: 300px;
  }

  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>

<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
